﻿namespace Site2AppWP8.Config
{
    using System;
    using System.Runtime.Serialization;
    using Newtonsoft.Json;

    [DataContract]
    public class WebConfig
    {
        public WebConfig() 
        {
        }

        public WebConfig(string jsonConfigFile)
        {
        }

        public Uri BaseURL 
        {
            get 
            { 
                return new Uri(this.BaseURLString);
            }
        }

        [DataMember(Name = "baseURL")]
        public string BaseURLString { get; set; }

        public Uri MobileBaseURL
        { 
            get 
            { 
                return new Uri(this.MobileURLString);
            } 
        }

        [DataMember(Name = "mobileURL")]
        public string MobileURLString { get; set; }

        [DataMember(Name = "share")]
        public ShareConfig Share { get; set; }

        [DataMember(Name = "secondaryPin")]
        public PinConfig Pin { get; set; }

        [DataMember(Name = "appBar")]
        public AppBarConfig AppBar { get; set; }

        [DataMember(Name = "navBar")]
        public NavBarConfig NavBar { get; set; }

        [DataMember(Name = "notifications")]
        public NotificationConfig Notifications { get; set; }

        [DataMember(Name = "logging")]
        public LoggingConfig Logging { get; set; }

        [DataMember(Name = "redirects")]
        public RedirectConfig Redirects { get; set; }

        [DataMember(Name = "settings")]
        public SettingsConfig Settings { get; set; }

        [DataMember(Name = "styles")]
        public StylesConfig Styles { get; set; }

        [DataMember(Name = "search")]
        public SearchConfig Search { get; set; }

        [DataMember(Name = "styleTheme")]
        public string StyleTheme { get; set; }

        [DataMember(Name = "livetile")]
        public LiveTileConfig LiveTile { get; set; }

        [DataMember(Name = "localXHRInterceptor")]
        public LocalXHRInterceptorConfig LocalXHRInterceptor { get; set; }

        public static WebConfig CreateConfig(string sourceJSON)
        {
            return JsonConvert.DeserializeObject<WebConfig>(sourceJSON);
        }

        public static string CreateJson(WebConfig config)
        {
            return JsonConvert.SerializeObject(config);
        }

        public Uri GetBaseUrl()
        {
            if (this.MobileURLString != string.Empty && Uri.IsWellFormedUriString(this.MobileURLString, UriKind.Absolute))
            {
                return this.MobileBaseURL;
            }
            else if (Uri.IsWellFormedUriString(this.BaseURLString, UriKind.Relative))
            {
                return new Uri(this.BaseURLString, UriKind.Relative);
            }
            else if (Uri.IsWellFormedUriString(this.BaseURLString, UriKind.Absolute))
            {
                return this.BaseURL;
            }

            throw new InvalidOperationException("baseURL or mobileURL must be a valid and well formed Uri string");
        }

        private bool GetJSONBool(string boolVal)
        {
            return boolVal == "true";
        }
    }
}
